#!/usr/bin/python2.5
# Copyright 2009 Joe LaPenna
"""Display user and product information."""

import logging
import os
import urllib

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import model


class User(webapp.RequestHandler):
  """Request to show user information."""

  def get(self):
    """Handle GET http method by returning a user page."""
    logging.debug('User request for: %s', self.request.path)
    user = User.setdefault(users.get_current_user())
    logging.debug('user: %s', user)

    template_values = {
        'nickname': users.get_current_user().nickname(),
        'products': user.get_products(),
    }
    path = os.path.join(os.path.dirname(__file__),
                                        '../templates/user.html')
    self.response.out.write(template.render(path, template_values))

  @staticmethod
  def setdefault(appengine_user):
    """Provided users.User, get User or create one."""
    user = model.User.find(appengine_user)
    if user is None:
      user = model.User(user=appengine_user, user_id=appengine_user.user_id())
      user.put()
      logging.info('Created new User: %s', user)
    return user
